package workforce.limousine.struts;

import limousine.struts.form.ProfileForm;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import limousine.util.SelectItem;
import limousine.vo.Users;
import limousine.util.Service;
import limousine.util.Token;
import limousine.util.Func;

public class UsersInsertInit extends Action {

    private Func func = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        System.out.println("***UsersInsertInit");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
        /*if (!user.getRole_name().equals("Admin")) {
        System.out.println("--Not Admin Hack--");
        return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        session.removeAttribute(Token.USERS);
        session.removeAttribute(Token.USERS_ROLE_LIST);
        session.removeAttribute(Token.USERS_GENDER_LIST);
        session.removeAttribute(Token.USERS_ENABLE_LIST);
        session.removeAttribute(Token.USERS_DATE_LIST);
        session.removeAttribute(Token.USERS_MONTH_LIST);
        session.removeAttribute(Token.USERS_YEAR_LIST);


        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        ProfileForm f = (ProfileForm) form;

        f.setPreviewImage("");
        f.setUserName("");
        f.setName("");
        f.setPhone("");
        f.setGender("");
        f.setShowBirthday("1");
        f.setShowEmail("1");
        f.setEmail("");
        f.setFacebook("");
        f.setTwitter("");
        f.setBirthday("");
        

        List<SelectItem> roleList = service.getRoleListByPrivilege(ds, user.getRoleId(), "", true);
        //roleList = service.setListHaveSelectOne(roleList, "");
        session.setAttribute(Token.USERS_ROLE_LIST, roleList);

        List<SelectItem> genderList = service.getGenderList("");
        //genderList = service.setListHaveSelectOne(genderList, "");
        session.setAttribute(Token.USERS_GENDER_LIST, genderList);

        List<SelectItem> enableList = service.getEnableList("");
        //enableList = service.setListHaveSelectOne(enableList, "");
        session.setAttribute(Token.USERS_ENABLE_LIST, enableList);


        List<SelectItem> dateList = service.getDateList("", false);
        session.setAttribute(Token.USERS_DATE_LIST, dateList);

        List<SelectItem> monthList = service.getMonthList("", false);
        session.setAttribute(Token.USERS_MONTH_LIST, monthList);

        List<SelectItem> yearList = service.getYearList("", false);
        session.setAttribute(Token.USERS_YEAR_LIST, yearList);


        // service.updateControlPanel(session, ds, "1");//update control panel display

        return mapping.findForward(Token.SUCCESS);
    }
}
